package com.example.ktapplication.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.example.ktapplication.adapter.MusicListAdapter
import com.example.ktapplication.base.BaseTitleRvFragment
import com.example.ktapplication.bean.Media
import com.example.ktapplication.enum.StateLayoutMode
import com.example.ktapplication.model.SystemViewModel
import com.example.ktapplication.utils.LogHelper
import com.example.ktapplication.utils.event.Event
import com.example.ktapplication.utils.event.EventBusUtil
import com.example.ktapplication.utils.toJson
import com.google.firebase.crashlytics.buildtools.reloc.com.google.common.eventbus.Subscribe

class MusicListFragment : BaseTitleRvFragment() {

    private val viewModel by lazy {
        ViewModelProvider(this)[SystemViewModel::class.java]
    }

    private val musicPlayerFragment by lazy {
        MusicPlayerFragment()
    }

    private var curMedia: Media? = null

    private val musicListAdapter by lazy {
        MusicListAdapter().apply {
            itemCallBack = object : MusicListAdapter.OnItemClickListener {
                override fun onItemClick(media: Media) {
                    curMedia = media
                    sendPlayMusic(media)
                }
            }
        }
    }

    override fun isRegisterEvent(): Boolean {
        return true
    }

    override fun initAdapter(): RecyclerView.Adapter<RecyclerView.ViewHolder> {
        return musicListAdapter
    }

    override fun onDate(savedInstanceState: Bundle?) {
        viewModel.mediaData.observe(this) {
            LogHelper.e("---ext ${javaClass.simpleName} mediaData >>> ${it.toJson()}")
            refreshList(it)
        }
        viewModel.addViewModelState {
            if (it != StateLayoutMode.CONTENT) {
                refreshList(mutableListOf())
            }
        }
    }

    private fun refreshList(it: MutableList<Media>) {
        musicListAdapter.addData(it)
        loadDataFinish()
        sendPlayMusic(null)
    }

    @SuppressLint("ResourceType")
    override fun onViewBing() {
        super.onViewBing()
        addMusicPlayerFm()
    }

    override fun loadData() {
        viewModel.getMediaData()
    }

    override fun loadMoreData() {
        loadDataFinish()
    }

    private fun sendPlayMusic(media: Media?) {
        EventBusUtil.eventBus.post(Event.PlayMusic(media))
    }

    @Subscribe
    fun onGetMusicMenu(event: Event.GetMusicMenu) { //发送播放列表
        EventBusUtil.eventBus.post(Event.MusicMenu(curMedia, musicListAdapter.medias))
    }

    @SuppressLint("ResourceType")
    fun addMusicPlayerFm() {
        // 创建一个新的 FrameLayout 作为 Fragment 容器
        val fragmentContainer = FrameLayout(requireContext()).apply {
            id = View.generateViewId() // 生成唯一 ID
            layoutParams = FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT
            ).apply { // 设置大小
                gravity = Gravity.BOTTOM // 居中
            }
        }
        // 把新创建的 FrameLayout 添加到根布局
        binding.statusLayout.apply {
            addView(fragmentContainer, childCount) //添加、且最上层显示
        }
        // 加载 Fragment
        showFragment(musicPlayerFragment, fragmentContainer.id)
    }

}
